package cn.jekun.web.shop.service;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.D;
import cn.jekun.component.base.validator.annotation.NotBlank;
import cn.jekun.component.core.bean.gen.RsMessage;
import cn.jekun.component.core.service.gen.RsMessageHisService;
import cn.jekun.component.core.service.gen.RsMessageService;
import cn.jekun.web.shop.bean.Message;

@Service(PushMessageService.BEAN_NAME)
public class PushMessageService extends ShopBaseService{

	public static final String BEAN_NAME="cn.jekun.web.shop.service.PushMessageService";
	public static final String NAMESPACE="ACTION_RS_MESSAGE";
	
	@Resource(name = RsMessageService.BEAN_NAME)
	RsMessageService rsMessageService;
	
	@Resource(name = RsMessageHisService.BEAN_NAME)
	RsMessageHisService rsMessageHisService;
	
	@Override
	public String getNamespace() {
		return NAMESPACE;
	}
	
	public List<Message> queryPushMessageList(@RsMessage.AnnoShopId String shopId,@D String beginDate,
			@D String endDate,@RsMessage.AnnoTitle String title,@RsMessage.AnnoMessage String content,@NotBlank Page page){
		
		String SQL = "";
		if("".equals(beginDate) && !"".equals(endDate)){
			SQL +=" and a.create_time"
					+ "<= to_date('"+beginDate.toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(beginDate) && "".equals(endDate)){
			SQL +=" and a.create_time"
					+ ">= to_date('"+beginDate.toString().trim()+"','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(beginDate) && beginDate!=null 
				&& !"".equals(endDate) && endDate!=null){
			SQL +=" and a.create_time >= to_date('"+beginDate.toString().trim()+"','yyyy/MM/dd hh24:mi:ss')"
					+ " and a.create_time <= to_date('"+endDate.toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')";
		}
		
		if(title!=null && title.toString().length()>0){
			SQL +=" and a.title like '%"+title.toString()+"%'"; 
		}
		
		if(content!=null && content.toString().length()>0){
			SQL +=" and a.message like '%"+content.toString()+"%'"; 
		}
		
		
		Map map=new HashMap();
		map.put("shopId", shopId);
		SqlUtis.prepareSql(map,SQL);
			
		return this.list(map, page);
		
	}
	
	/**
	 * 是否存在信息表
	 * @param messageId 消息代码
	 * @return
	 */
	 @SuppressWarnings({ "unchecked", "rawtypes" })
	public boolean exists(String title,String shopId){
		Map queryMap = new HashMap();
		queryMap.put(RsMessage.COL_TITLE, title);
		queryMap.put(RsMessage.COL_SHOP_ID, shopId);
		SqlUtis.prepareSql(queryMap
				,SqlUtis.getSQLRequired(queryMap, RsMessage.COL_TITLE)
				,SqlUtis.getSQLRequired(queryMap, RsMessage.COL_SHOP_ID)
				);
		return this.rsMessageHisService.exists(queryMap);
	}
}
